package com.wsfk.ssm.path;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.lang.StringUtils;

import com.wsfk.ssm.entity.HandBook;
import com.wsfk.ssm.entity.Picture;
import com.wsfk.ssm.entity.User;

public class CallPath {

	private static int MAX_BOOK_SIZE = 10;
	private String pre;
	private String bookPath;
	private String file;
	
	public CallPath httpPre(POPS pops) {
		this.pre = pops.getHttpViewPath();
		return this;
	}

	public CallPath savePre(POPS pops) {
		pre = pops.getSavePath();
		return this;
	}
	
	public CallPath bookWay(User user, HandBook book) {
		this.bookPath = user.getUserName()+"/book"+book.getId();
		return this;
	}

	public CallPath imageWay(User user, HandBook book) {
		bookPath = user.getUserName()+"/book"+book.getId()+"/images";
		file = ".jpg";
		return this;
	}

	public CallPath zipWay(User user, HandBook book) {
		this.bookPath=user.getUserName();
		this.file = "book"+book.getId()+".zip";
		return this;
	}

	public CallPath codeWay(User user, HandBook book) {
		this.bookPath = user.getUserName()+"/towCodes";
		this.file = book.getId()+".png";
		return this;
	}

	public CallPath iconWay(User user, HandBook book) {
		this.bookPath = user.getUserName()+"/book"+book.getId()+"/images";
		this.file = "icon"+book.getId()+".jpg";
		return this;
	}

	public List builds(int size) {
		
		List list = new ArrayList();
		for(int i =0;i<size;i++){
			String temp = build(i);
			list.add(temp);
		}
		return list;
	}
	public List builds() {
		return builds(MAX_BOOK_SIZE);
	}
	public String build() {
		return build(-1);
	}

	private String build(int i) {
		List<String> list = new ArrayList<String>();
		list.add(pre);
		list.add(bookPath);
		if(i<0){
			list.add(file);			
		}else{
			list.add((i+1)+file);
		}
		CollectionUtils.filter(list, PredicateUtils.notNullPredicate());
		String p = StringUtils.join(list,"/");
		return p;
	}


	public List<String> builds(List<String> place) {
		List<String> result = new ArrayList<String>();
		for(int i=0;i<place.size();i++){
			if(StringUtils.startsWithIgnoreCase(place.get(i), "y")){
				result.add(build(i));
			}
		}
		return result;
	}


	public CallPath picWay(Picture pic) {
		String picPath = pic.getFileDir();
		String[] arr = StringUtils.split(picPath, "\\|/");
		LinkedList<String> list = new LinkedList<String>(Arrays.asList(arr));
		this.file = list.removeLast();
		this.bookPath = StringUtils.join(list.toArray(),"/");
		return this;
	}


	public CallPath readyImageWay(String userId, String bookId,String imageId) {
		//临时文件的相对地址
		this.bookPath = userId+"/book"+bookId;
		this.file = imageId+".jpg";
		return this;
	}


	public CallPath clearUpImageWay(User user, HandBook handBook, String no) {
		// PIC文件的相对地址
		this.bookPath = user.getUserName()+"/book"+handBook.getId()+"/images";
		this.file = no+".jpg";
		return this;
	}

}